package com.sjq.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sjq.common.dao.ApplicationSystemDao;
import com.sjq.common.dao.MenuManagerDao;
import com.sjq.common.entity.ApplicationSystem;
import com.sjq.common.entity.Menu;
import com.sjq.common.entity.Result;
import com.sjq.common.service.MenuManagerService;
@Service
public class MenuManagerServiceImpl implements MenuManagerService {
	
	//菜单管理 dao
	@Resource
	private MenuManagerDao menuManagerDao;
	
	@Resource
	private ApplicationSystemDao applicationSystemDao;

	@Override
	public List<Menu> findMenuList() {
		List<Menu> menuList=menuManagerDao.findMenuList();
		return menuList;
	}

	@Override
	public String saveMenu(Menu menu) {
		String msg="";
		int c=menuManagerDao.saveMenu(menu);
		if(c>0){
			msg="{\"status\":0,\"msg\":\"success\"}";
		}else{
			msg="{\"status\":1,\"msg\":\"error\"}";
		}
		return msg;
	}

	@Override
	public List<Menu> findBySystemId(String systemId) {
List<Menu> reults=new ArrayList<Menu>();
		
		ApplicationSystem sys=new ApplicationSystem();
		sys.setXxzjbh(systemId);
		List<ApplicationSystem> sysList=applicationSystemDao.find(sys);
		
		if(sysList.size()>0){
			for(int i=0;i<sysList.size();i++){
				//创建根节点
				Menu rootMenu=new Menu();
				rootMenu.setMenuId(i+"");
				rootMenu.setParentMenuId("0");
				rootMenu.setMenuName(sysList.get(i).getXtmc());
				rootMenu.setSystemId(sysList.get(i).getXxzjbh());
				rootMenu.setOrderno(i+"");
				reults.add(rootMenu);
			
				List<Menu> menuList=menuManagerDao.findBySystemId(systemId);
				reults.addAll(menuList);
				
			}
		}else{
			throw new RuntimeException("未查找到系统信息!");
		}
				
		
		return reults;
	}

	@Override
	public String findByParentMenuId(String  systemId,String parentMenuId) {
		List<Menu> menuList=menuManagerDao.findByParentMenuId(systemId,parentMenuId);
		String json =JSONArray.fromObject(menuList).toString();
		return json;
	}

	@Override
	public Menu findByXxzjbh(String xxzjbh) {
		Menu meun=menuManagerDao.findByXxzjbh(xxzjbh);
		
		return meun;
	}

	@Override
	public String updateMenu(Menu menu) {
		int i=menuManagerDao.updateMenu(menu);
		String msg="";
		if(i>0){
			msg="{\"status\":0,\"msg\":\"success\"}";
		}else{
			msg="{\"status\":1,\"msg\":\"error\"}";
		}
		return msg;
	}

	@Override
	public String deleteMenu(String xxzjbh) {
		int i=menuManagerDao.deleteMenu(xxzjbh);
		String msg="";
		if(i>0){
			msg="{\"status\":0,\"msg\":\"success\"}";
		}else{
			msg="{\"status\":1,\"msg\":\"error\"}";
		}
		return msg;
	}
	
	@Override
	@Transactional
	public Result updateDrop(String xxzjbh,String parentMenuId) {
		Result result=new Result();
		try {
			//根据信息主键编号 查询菜单
			Menu dropRootNode=menuManagerDao.findByXxzjbh(xxzjbh);
			//更新拖拽节点
			String dropChildNodesNewParentMenuId= menuManagerDao.updateDropRootNode(xxzjbh, parentMenuId);
			if(dropChildNodesNewParentMenuId!=null&&!"".equals(dropChildNodesNewParentMenuId)){
				String systemId=dropRootNode.getSystemId();
				String dropChildNodesOldParentMenuId=dropRootNode.getMenuId();
				//更新拖拽子菜单
				int c=menuManagerDao.updateDropChildsNode(dropChildNodesOldParentMenuId, dropChildNodesNewParentMenuId, systemId);
				if(c>0){
					result.setStatus(0);
					result.setMsg("拖拽成功");
				}else {
					List<Menu> childs= menuManagerDao.findByParentMenuId(systemId,dropChildNodesOldParentMenuId);
					if(childs.size()>0){
						throw new RuntimeException("更新拖拽子菜单失败");
					}else{
						result.setStatus(0);
						result.setMsg("拖拽成功");
					}
				}
				
			}else{
				throw new RuntimeException("未获取新的菜单id");
			}
		} catch (Exception e) {
			
			throw new RuntimeException(e);
		}
		
		
		return result;
	}

	@Override
	@Transactional
	public Result updateDropOrderNo(JSONArray jsonArray) {
		Result result =new Result();
		try {
			List<Object[]> bathArgs=new ArrayList<Object[]>();	
			for(int i=0;i<jsonArray.size();i++){
				Object[] parpams=new Object[2];
				parpams[0]=jsonArray.getJSONObject(i).get("orderno");
				parpams[1]=jsonArray.getJSONObject(i).get("xxzjbh");
			
				bathArgs.add(parpams);
			}
			int[] arr=menuManagerDao.updateDropOrderNo(bathArgs);
			
			result.setStatus(0);
			result.setMsg("更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return result;
	}
}
